//
//  SLProductItemComponent.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/19.
//  
  

#import "SLProductItemComponent.h"
#import <YYKit.h>
#import <Masonry.h>


@implementation SLProductItemComponent

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.layer.cornerRadius = 20;
        self.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        self.layer.borderWidth = 1.5;
        self.backgroundColor = UIColor.whiteColor;
        
        [self addSubview:self.avatar];
        [self addSubview:self.productName];
        [self addSubview:self.apply];
        [self addSubview:self.horizontalDevider];
        [self addSubview:self.creditLimit];
        [self addSubview:self.verticalDevider];
        [self addSubview:self.creditLimitMoney];
        [self.avatar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.top.equalTo(self).offset(10);
            make.width.height.mas_equalTo(60);
        }];
        [self.productName setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
        [self.productName setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
        [self.productName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.avatar.mas_right).offset(10);
            make.centerY.equalTo(self.avatar);
        }];
        [self.apply mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.productName.mas_right).offset(10);
            make.right.equalTo(self).inset(10);
            make.centerY.equalTo(self.avatar);
            make.height.mas_equalTo(45);
            make.width.mas_equalTo(self.apply.intrinsicContentSize.width+37*2);
        }];
        [self.horizontalDevider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self).inset(14);
            make.top.equalTo(self.avatar.mas_bottom).offset(10);
            make.height.mas_equalTo(2);
        }];
        
        [self.creditLimit mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).inset(14);
            make.top.equalTo(self.horizontalDevider.mas_bottom).offset(0);
            make.bottom.equalTo(self);
        }];
        [self.verticalDevider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.horizontalDevider.mas_bottom).offset(0);
            make.bottom.equalTo(self);
            make.centerX.equalTo(self);
            make.width.mas_equalTo(2);
        }];
        [self.creditLimitMoney mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).inset(14);
            make.centerY.equalTo(self.creditLimit);
            make.bottom.equalTo(self);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.avatar.layer.cornerRadius = CGRectGetHeight(self.avatar.bounds)*0.5;
    self.apply.layer.cornerRadius = CGRectGetHeight(self.apply.bounds)*0.5;
}

- (UIImageView *)avatar {
    if (!_avatar) {
        _avatar = [UIImageView new];
        _avatar.contentMode = UIViewContentModeScaleAspectFit;
        _avatar.image = [UIImage imageNamed:@"icon_circle_gray"];
    }
    return _avatar;
}
- (UILabel *)productName {
    if (!_productName) {
        _productName = [UILabel new];
        _productName.textColor = UIColorHex(#666666);
        _productName.font = aspectDefaultFont(14);
        _productName.text = @"Product name";
        _productName.numberOfLines = 0;
    }
    return _productName;
}
- (UILabel *)apply {
    if (!_apply) {
        _apply = [UILabel new];
        _apply.textColor = UIColorHex(#666666);
        _apply.font = aspectDefaultFont(14);
        _apply.text = @"Apply";
        _apply.backgroundColor = UIColorHex(#FDC5E0);
        _apply.textAlignment = NSTextAlignmentCenter;
        _apply.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _apply.layer.borderWidth = 1.5;
        _apply.clipsToBounds = YES;
    }
    return _apply;
}
- (DashedLineView *)horizontalDevider {
    if (!_horizontalDevider) {
        _horizontalDevider = [DashedLineView new];
    }
    return _horizontalDevider;
}
- (UILabel *)creditLimit {
    if (!_creditLimit) {
        _creditLimit = [UILabel new];
        _creditLimit.textColor = UIColorHex(#666666);
        _creditLimit.font = aspectDefaultFont(14);
        _creditLimit.text = @"Credit limit (₱)";
        _creditLimit.numberOfLines = 0;
    }
    return _creditLimit;
}
- (DashedLineView *)verticalDevider {
    if (!_verticalDevider) {
        _verticalDevider = [DashedLineView new];
        _verticalDevider.direction = DLDirectionVertical;
    }
    return _verticalDevider;
}
- (UILabel *)creditLimitMoney {
    if (!_creditLimitMoney) {
        _creditLimitMoney = [UILabel new];
        _creditLimitMoney.textColor = UIColorHex(#666666);
        _creditLimitMoney.font = aspectDefaultFont(14);
        _creditLimitMoney.text = @"50,000";
        _creditLimitMoney.textAlignment = NSTextAlignmentRight;
    }
    return _creditLimitMoney;
}
@end
